package com.gao.controller;

import com.gao.service.IPaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: suGar
 * @Date: 2022/11/13 15:00
 */
@RequestMapping("order")
@RestController
@Slf4j
public class OrderController {

    private final IPaymentService paymentService;

    @Autowired
    public OrderController(@Qualifier("IPaymentService") IPaymentService paymentService) {
        this.paymentService = paymentService;
    }

    @GetMapping("paymentInfo_OK/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        return paymentService.paymentInfo_OK(id);
    }

    @GetMapping("paymentInfo_TimeOut/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
    })
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        return paymentService.paymentInfo_TimeOut(id);
    }

    public String paymentInfo_TimeOutHandler(Integer id) {
        return "线程池：" + Thread.currentThread().getName() + "    paymentInfo_Handler:    网络跑丢了:id：" + id;
    }

    public String defaultFallback() {
        return "线程池：" + Thread.currentThread().getName() + "    defaultFallback:    网络跑丢了:id：";
    }
}
